// TranslateGeometricElement.java
// by Alexandre Martins, 2013
// MAC 5857 - Prof. Leonidas

package example.ilm.model;

import ilm.framework.assignment.model.DomainAction;
import ilm.framework.domain.DomainModel;

public class TranslateGeometricElement extends DomainAction {

    private IlmDomainModel _domain;
    private GeometricElement geometricElement;
    private double dx;
    private double dy;

    private static int cnt = 0;
    public TranslateGeometricElement() {
        super("translateGeometricElement[" + (++cnt) + "]", "translated a geometric element");
        this.dx = 0;
        this.dy = 0;
    }

    public void setGeometricElement(GeometricElement geomel) {
        geometricElement = geomel;
    }

    public void setTranslation(double dx, double dy) {
        this.dx = dx;
        this.dy = dy;
    }

    /**
     * Defines the DomainModel on which this action can happen. It must be
     * converted to IlmDomainModel so this class can call its specific methods.
     * 
     * @param an
     *            IlmDomainModel
     */
    public void setDomainModel(DomainModel model) {
        _domain = (IlmDomainModel) model;
    }

    /**
     * Method that is called when this action is executed.
     */
    protected void executeAction() {
        _domain.translateGeometricElement(_currentState, geometricElement, dx, dy);
    }

    /**
     * Method that is called when this action is undone.
     */
    protected void undoAction() {
        _domain.translateGeometricElement(_currentState, geometricElement, -dx, -dy);
    }

    /**
     * Compares this to another DomainAction.
     */
    public boolean equals(DomainAction a) {
        TranslateGeometricElement b = (TranslateGeometricElement)a;
        return (getName().equals(a.getName()) && getDescription().equals(a.getDescription()) && geometricElement.equals(b.geometricElement) && dx == b.dx && dy == b.dy);
    }

    /**
     * This method is used for converting this object into string
     * 
     * @see example.ilm.model.IlmDomainConverter
     */
    public String toString() {
        return "" + 
            "<translategeometricelement>" + 
                "<name>" + getName() + "</name>" + 
                "<description>" + getDescription() + "</description>" + 
                "<dx>" + dx + "</dx>" + 
                "<dy>" + dy + "</dy>" + 
                "<geometricelement>" + 
                    geometricElement.toString() + 
                "</geometricelement>" + 
            "</translategeometricelement>";
    }
}
